﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
namespace MMDemo
{
    public partial class SysDemoForm : Form
    {
        /// <summary>
        /// 数据结构的接口
        /// </summary>
        private InterfaceDataStructs StructsInterface = null;
        public static Thread AnimationThread;
        public SysDemoForm()
        {
            InitializeComponent();
            SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }
        ~SysDemoForm()
        {
            if (AnimationThread != null)
            {
                AnimationThread.Abort();
            }
        }
        const int WM_NCLBUTTONDOWN = 0x00A1;
        const int HTCAPTION = 2;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCLBUTTONDOWN && m.WParam.ToInt32() == HTCAPTION)
            {
                return;
            }
            if (m.Msg == 0xA3)//双击标题栏
            {
                base.WndProc(ref m);

                StructsInterface.ResetBuffer(Animation_PicBox.Width, Animation_PicBox.Height);
                Refresh();

                return;
            }
            base.WndProc(ref m);
        }

        private void DemoForm_Load(object sender, EventArgs e)
        {
            if (null == StructsInterface)
            {
                this.Animation_PicBox.Image = PublicDataClass.DemoPicture;
                this.Animation_PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
                switch (PublicDataClass.functionflag/10)
                {
                    case 1: StructsInterface = new AnimationList(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                                 Animation_PicBox.Height); break;
                    case 2: StructsInterface = new AnimationLink(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                               Animation_PicBox.Height); break;//利用构造函数传递演示框的信息
                    case 3: StructsInterface = new AnimationQueue(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                           Animation_PicBox.Height); break;
                    case 4: StructsInterface = new AnimationStack(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                           Animation_PicBox.Height); break;
                    case 5: StructsInterface = new AnimationBinTree(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                               Animation_PicBox.Height); break;
                    case 6: StructsInterface = new AnimationGraphs(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                                  Animation_PicBox.Height); break;
                    case 7: StructsInterface = new AnimationSort(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                              Animation_PicBox.Height); break;
                    case 8: StructsInterface = new AnimationSearch(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                          Animation_PicBox.Height); break;
                    case 9: StructsInterface = new AnimationTree(this, new DrawHandler(RefreshPictureBox), Animation_PicBox.Width,
                                      Animation_PicBox.Height); break;
                }
            }
        }
        private void RefreshPictureBox()
        {
            Animation_PicBox.Invalidate(Animation_PicBox.Region);
        }

        private void Animation_PicBox_Paint(object sender, PaintEventArgs e)
        {
                if (null == StructsInterface)
                {
                    return;
                }
                e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                e.Graphics.DrawImage(StructsInterface.GetBufferBmp(),
                Animation_PicBox.ClientRectangle,
                Animation_PicBox.ClientRectangle,
               GraphicsUnit.Pixel);
        }
        
        public void Excute()
        {
            AnimationThread = new Thread(new ThreadStart(StructsInterface.Animation));
            AnimationThread.Start();
        }

        public void ResetDemo()
        {
            StructsInterface.ResetData();
        }
        public void SuspedListThread()
        {
            AnimationThread.Suspend();
        }
        public void Init()
        {
            StructsInterface.InitData();
        }
        public void SetSpeed(float scale)
        {
            StructsInterface.SetSpeed(scale);
        }
    }
}
